layui.define([ 'form', 'layer' ], function(exports) {
    var $ = layui.$,form = layui.form;
    var api = {
        update : function(form, callback) {
            Lib.submitForm(Common.ctxPath + "/space/member/update.json", form, {}, callback);
        },
        reg : function(form, callback) {
            Lib.submitForm(Common.ctxPath + "/space/member/reg.json", form, {}, callback);
        },
        login : function(form, callback) {
            Lib.submitForm(Common.ctxPath + "/space/login.json", form, {}, callback);
        },
        logout : function(callback) {
            Common.post("/core/member/logout.json", {}, function() {
                callback();
            })
        },
        modifyInfo : function(form, callback) {
            Lib.submitForm(Common.ctxPath + "/space/modifyMemberInfo.json", form, {}, callback);
        },
        changePassword : function(form, callback) {
            Lib.submitForm(Common.ctxPath + "/space/changePassword.json", form, {}, callback)
        },
        articleSave : function(form, callback) {
            Lib.submitForm(Common.ctxPath + "/space/article/update.json", form, {}, callback)
        },
        articleDel : function(ids, callback) {
            Common.post(Common.ctxPath + "/space/article/delete.json", {"ids": ids},callback);
        },
        topBgSave : function(bgId,callback) {
            Common.post(Common.ctxPath + "/space/member/topBg/save.json", {"bgId": bgId},callback);
        },
        topBgDel : function(callback) {
            Common.post(Common.ctxPath + "/space/member/topBg/delete.json", {},callback);
        },
        initMemberTypeItem(config) {
            var that = this;
            var el = config.memberTypeElement;
            var selected = config.memberTypeValue;
            $.post("/core/dict/viewChildren.json",{type:"member_type",value:"type"},function(resp) {
                var html = '<div class="member_type">';
                $.each(resp.data,function() {
                    html += '<input type="radio" name="type" lay-verify="required1|member_type" lay-reqText="用户类型必须输入" ' + (this.value == selected[0] ? 'checked' :'') + ' lay-filter="type" value="' + this.value + '" title="' + this.name + '">';
                });
                html += '</div>';
                $(el).html(html);

                form.render();
                if (config.memberTypeValue[0].length > 0) {
                    that.initMemberType1Item(config);
                }

            },"json");
        },
        initMemberType1Item(config,defaultValue) {
            var that = this;
            var el = config.memberTypeElement;
            var selected = config.memberTypeValue;
            var value = defaultValue ? defaultValue : selected[0];
            $.post("/core/dict/viewChildren.json",{type:"member_type",value:value},function(resp) {
                var html = '<div class="member_type1 member_type_added">';
                $.each(resp.data,function() {
                    html += '<input type="radio" name="type1" lay-verify="required1|member_type1" lay-reqText="用户类型1必须输入" ' + (this.value == selected[1] ? 'checked' :'') + ' lay-filter="type1" value="' + this.value + '" title="' + this.name + '">';
                });
                html += '</div>';
                $(".member_type1").remove();
                $(".member_type2").remove();
                $(el).closest(el).append(html);
                form.render();

                if (defaultValue == undefined) {
                    that.initMemberType2Item(config);
                }
            },"json");
        },
        initMemberType2Item(config,defaultValue) {
            var that = this;
            var el = config.memberTypeElement;
            var selected = config.memberTypeValue;
            var value = defaultValue ? defaultValue : selected[1];
            $.post("/core/dict/viewChildren.json",{type:"member_type",value:value},function(resp) {
                var html = '<div class="member_type2 member_type_added">';
                $.each(resp.data,function() {
                    html += '<input type="checkbox" name="type2Value" lay-verify="member_type2" ' + (selected[2].indexOf(this.value) != -1 ? 'checked' :'') + ' lay-filter="type2" value="' + this.value + '" title="' + this.name + '">';
                });
                html += '</div>';
                $(".member_type2").remove();
                $(el).closest(el).append(html);
                form.render();
            },"json");
        },
        initMemberType(config) {
            var that = this;
            var el = config.memberTypeElement;
            var values = config.memberTypeValue;
            //console.log("memberApi.js.initMemberType")
            if (el.length > 0) {
                this.initMemberTypeItem(config);
            }

            form.on('radio(type)',function(data) {
                var value = data.value;
                that.initMemberType1Item(config,value);
            });
            form.on('radio(type1)',function(data) {
                var value = data.value;
                that.initMemberType2Item(config,value);
            })
        },
        loginNameVerify(loginName) {
            var params = {"loginName":loginName};
            var result;
            //console.log("memberApi.loginNameVerify start" + params)
            $.ajax({
                url: "/core/member/loginNameVerify.json",
                type: "POST",
                traditional:false,
                async: false,
                data: params,
                success: function(data) {
                    result = data;
                }
            });
            return result;
        },
        domainVerify(domain) {
            var params = {"domain":domain};
            var result;
            //console.log("memberApi.domainVerify" + params);
            $.ajax({
                url: "/core/member/domainVerify.json",
                type: "POST",
                async: false,
                data: params,
                success: function(data) {
                    result = data;
                }
            });
            return result;
        },
        passwordVerify(password1,password2) {
            if (password1 != password2) {
                return "两次输入密码不正确";
            }
        }
    }
	exports('memberApi', api);
});